package TUILayer;//makes this class a part of the TUILayer package
import java.util.Scanner;
import ControlLayer.*;//The TUILayer needs to know the ControlLayer
 

/**
 * User interface class to the AddressBook
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * (FEN): 2006.03.21: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */
public class AddressBookUI
{   
    private AddressCtr addrCtr;//reference to the ControlLayer
    
    //constructor which does nothing
    public AddressBookUI()
    {
        addrCtr = new AddressCtr();
    }
    
    public void start()
    {
        addressMenu();
    }
    
    private void addressMenu()
    {
        boolean exit = false;
        while(!exit)//that is: while exit is false
        {
            int choice = writeAddressBookMenu(); 
            if(choice == 1)
            {   //Show person info
                showPersonInfo();
                waitForInput();
            }
            else{
                if(choice == 2)
                {
                    //Add person
                    createPerson();
                }  
                else{
                    if(choice == 3)
                    {
                        //Remove person
                        deletePerson();
                    }  
                    else{
                        if(choice == 4)
                        {
                            //Update person
                            updatePerson();
                        }
                        else{
                            if(choice == 5)
                            {
                                //ListAllPersons();
                                showAllPersons();
                                waitForInput();
                            }
                            else{
                                exit = true;
                            }//end else
                        }//end else
                    }//end else
                }//end else
            }//end else
            
        }//end while
    }
    
    private int writeAddressBookMenu()
    {      // makes an object keyboard to read input from the screen
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** AddressBook Menu ***");
            System.out.println(" (1) Show information about person");
            System.out.println(" (2) Add person");
            System.out.println(" (3) Delete person");
            System.out.println(" (4) Update person");
            System.out.println(" (5) List all persons");
            System.out.println(" (6) Return");
            System.out.print("\n Make your choice: ");
            
            int choice = keyboard.nextInt();
            return choice;
    }
        
    //the method reads id for a person and return
    // 
    private long inputPersonsID()
    {   
        boolean ok = false;
        long id = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write Id:  ");
           try{
             id = keyboard.nextLong();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("You must enter a number -  try again");
             String input = keyboard.nextLine();
           }
        }//end while
        return id;
    }
    
    private String inputName()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Name:  ");
         String name = keyboard.nextLine();
         return name;
    }
   
    private int inputPostalCode()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Postal Code:  ");
         int code = Integer.parseInt(keyboard.nextLine());
         return code;
    }
    
    private String inputCity()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write City:  ");
         String city = keyboard.nextLine();
         return city;
    } 
    
    private String inputPhone()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Phone Number:  ");
         String phone = keyboard.nextLine();
         return phone;
    } 
     
    private String inputAddress()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write Address: ");
        String address = keyboard.nextLine();
        return address;
    }
    
    private void createPerson()
    {
        addrCtr.addPerson(inputName(), inputAddress(), inputPostalCode(), inputCity(), inputPhone());
    }
    
    private void deletePerson()
    {
        addrCtr.deletePerson(inputPhone());
    }
    
    private void showPersonInfo()
    {
        addrCtr.printInfo(inputPhone());
    }
    
    private int updatePersonMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** Update Person Menu ***");
        System.out.println(" (1) Change name");
        System.out.println(" (2) Change address");
        System.out.println(" (3) Change postalCode");
        System.out.println(" (4) Change City");
        System.out.println(" (5) Change phonenumber");
        System.out.println(" (6) Return");
        System.out.print("\n Make your choice: ");
        
        int choice = keyboard.nextInt();
        return choice;
    }
    
    private void updatePerson()
    {
        boolean exit = false;
        while(!exit)
        {
            int choice = updatePersonMenu(); 
            if(choice == 1)
            {   
                addrCtr.changeName(inputPhone(), inputName());
            }
            else{
                if(choice == 2)
                {
                    addrCtr.changeAddress(inputPhone(), inputAddress());
                }  
                else{
                    if(choice == 3)
                    {
                        addrCtr.changePostalCode(inputPhone(), inputPostalCode());
                    }  
                    else{
                        if(choice == 4)
                        {
                            addrCtr.changeCity(inputPhone(), inputCity());
                        }
                        else{
                            if(choice == 5)
                            {
                                addrCtr.changePhone(inputPhone(), inputPhone());
                            }
                            else{
                                exit = true;
                            }//end else
                        }//end else
                    }//end else
                }//end else
            }//end else
            
        }
    }
    
    private void showAllPersons()
    {
        addrCtr.printAllInfo();
    }
    
    private void waitForInput()
    {
        Scanner keyboard = new Scanner(System.in);
    }
}
            
        
